cd "C:\Users\jmunoz\Dropbox\01 - Recerca\replication_sweden\Paper_datasets_and_figure_R_codes\Replication WP"

**------------------------------**
*---Preparation Data 1921-60----**
**------------------------------**

import delimited "01 - Original data\With_1932.csv", clear 
save "03 - Working data\marital_class.dta", replace

foreach x in 1944 1948 1952 1956 1958 1960 {
import delimited "01 - Original data\Preprocessed_`x'.csv", varnames(1) clear 
save "03 - Working data\Preprocessed_`x'.dta", replace
}

use "03 - Working data\Preprocessed_1960.dta"
rename id id_old
gen id=""
replace id = "Eligible_Number_Man_Man" if id_old=="Man_Man_Eligible_Eligible"
replace id = "Turnout_Number_Man_Man" if id_old=="Man_Man_Turnout_Number"
replace id = "NA" if id_old=="Man_Married_Eligible_Eligible"
replace id = "NA" if id_old=="Man_Married_Turnout_Number"
replace id = "NA" if id_old=="Man_Married_Turnout_Percent"
replace id = "NA" if id_old=="Man_Single_Eligible_Eligible"
replace id = "NA" if id_old=="Man_Single_Turnout_Number"
replace id = "NA" if id_old=="Man_Single_Turnout_Percent"
replace id = "Eligible_Number_Woman_Other_fmember" if id_old=="Woman_Other_fmember_Eligible_Eligible"
replace id = "Turnout_Number_Woman_Other_fmember" if id_old=="Woman_Other_fmember_Turnout_Number"
replace id = "Turnout_Percent_Woman_Other_fmember" if id_old=="Woman_Other_fmember_Turnout_Percent"
replace id = "Eligible_Number_Woman_Professional" if id_old=="Woman_Professional_Eligible_Eligible"
replace id = "Turnout_Number_Woman_Professional" if id_old=="Woman_Professional_Turnout_Number"
replace id = "Turnout_Percent_Woman_Professional" if id_old=="Woman_Professional_Turnout_Percent"
replace id = "NA" if id_old=="Woman_Recently Married_Eligible_Eligi.."
replace id = "NA" if id_old=="Woman_Recently Married_Turnout_Number"
replace id = "NA" if id_old=="Woman_Recently Married_Turnout_Percent"
replace id = "Eligible_Number_Woman_Wives" if id_old=="Woman_Wives_Eligible_Eligible"
replace id = "Turnout_Number_Woman_Wives" if id_old=="Woman_Wives_Turnout_Number"
replace id = "Turnout_Percent_Woman_Wives" if id_old=="Woman_Wiveso_Eligible_Eligible"
replace id = "NA" if id_old=="Woman_Wiveso_Turnout_Number"
replace id = "NA" if id_old=="Woman_Wiveso_Turnout_Percent"
drop if id=="NA"
drop id_old
save "03 - Working data\Preprocessed_1960.dta", replace

clear
use "03 - Working data\marital_class.dta"
foreach x in 1944 1948 1952 1956 1958 1960 {
append using "03 - Working data\Preprocessed_`x'.dta"
}

replace id = subinstr(id, "Not_Elig_", "NotEligible_", .)
replace id = subinstr(id, "Other_fmember", "Otherfmember", .)
replace id= "Eligible_Number_Man_Allmen" if id== "Eligible_Man"
replace id= "Eligible_Number_Man_Allmen" if id== "Eligible_Number_Man_Man"
replace id= "Eligible_Number_Woman_Otherfmember" if id== "Eligible_Number_Woman_Otherfmember"
replace id= "Eligible_Number_Woman_Professional" if id== "Eligible_Number_Woman_Professional"
replace id= "Eligible_Number_Woman_Wives" if id== "Eligible_Number_Woman_Wives"
replace id= "Eligible_Number_Woman_Otherfmember" if id== "Eligible_Woman_Otherfmember"
replace id= "Eligible_Number_Woman_Professional" if id== "Eligible_Woman_Professionals"
replace id= "Eligible_Number_Woman_Total" if id== "Eligible_Woman_Total"
replace id= "Eligible_Number_Woman_Wives" if id== "Eligible_Woman_Wives"
replace id= "NotEligible_Number_Man_Allmen" if id== "NotEligible_Man"
replace id= "NotEligible_Number_Woman_Otherfmember" if id== "NotEligible_Woman_Otherfmember"
replace id= "NotEligible_Number_Woman_Professional" if id== "NotEligible_Woman_Professionals"
replace id= "NotEligible_Number_Woman_Total" if id== "NotEligible_Woman_Total"
replace id= "NotEligible_Number_Woman_Wives" if id== "NotEligible_Woman_Wives"
replace id= "Turnout_Number_Man_Allmen" if id== "Turnout_Man_Number"
replace id= "Turnout_Percent_Man_Allmen" if id== "Turnout_Man_Percent"
replace id= "Turnout_Number_Man_Allmen" if id== "Turnout_Number_Man_Man"
replace id= "Turnout_Number_Woman_Otherfmember" if id== "Turnout_Number_Woman_Otherfmember"
replace id= "Turnout_Number_Woman_Professional" if id== "Turnout_Number_Woman_Professional"
replace id= "Turnout_Number_Woman_Wives" if id== "Turnout_Number_Woman_Wives"
replace id= "Turnout_Percent_Man_Allmen" if id== "Turnout_Percent_Man_Man"
replace id= "Turnout_Percent_Woman_Otherfmember" if id== "Turnout_Percent_Woman_Otherfmember"
replace id= "Turnout_Percent_Woman_Professional" if id== "Turnout_Percent_Woman_Professional"
replace id= "Turnout_Percent_Woman_Wives" if id== "Turnout_Percent_Woman_Wives"
replace id= "Turnout_Number_Woman_Otherfmember" if id== "Turnout_Woman_Otherfmember_Number"
replace id= "Turnout_Percent_Woman_Otherfmember" if id== "Turnout_Woman_Otherfmember_Percent"
replace id= "Turnout_Number_Woman_Professional" if id== "Turnout_Woman_Professionals_Number"
replace id= "Turnout_Percent_Woman_Professional" if id== "Turnout_Woman_Professionals_Percent"
replace id= "Turnout_Number_Woman_Total" if id== "Turnout_Woman_Total_Number"
replace id= "Turnout_Percent_Woman_Total" if id== "Turnout_Woman_Total_Percent"
replace id= "Turnout_Number_Woman_Wives" if id== "Turnout_Woman_Wives_Number"
replace id= "Turnout_Percent_Woman_Wives" if id== "Turnout_Woman_Wives_Percent"


split id, p("_")

drop if id1=="NotEligible"
drop if id2=="Percent"
drop if id3=="Total"
drop if id4=="Total"

gen byte notnumeric = real(value)==.
tab value if notnum==1
replace value="" if value=="NA"
destring value, replace

encode id1, gen(vote)
drop id2
encode id3, gen(gender)
encode id4, gen(marital)

drop id1-notnumeric

split year_occup, p("_")
drop year_occup
rename year_occup2 occupation


drop if vote==.
destring occupation, replace 
destring year, replace
egen group=group(year gender marital occupation)
drop id
reshape wide value, i(group) j(vote)
rename value1 eligible
rename value2 voted
gen turnout=voted/eligible*100

save "03 - Working data\marital_class.dta", replace

*We add hisclass codes

use "01 - Original data\hisclass.dta"
fre Year_Occup
use "03 - Working data\marital_class.dta", clear
gen Year_Occup=string(year)+"_"+string(occupation)
merge m:1 Year_Occup using "01 - Original data\hisclass.dta"

encode Hisclass7, gen(hisclass7)
gen hisclass12=subinstr(Hisclass, "H", "",1)
destring hisclass12, replace
encode Descriptive, gen(hisclass7_label)

label define hisclass7_2 1"Upper Middle" 2"Middle" 3"Low Middle" 4"Farmers" 5"Industrial workers" 6"Unskilled Urban" 7"Low Skilled Farmers"
label values hisclass7 hisclass7_2

gen agriculture=hisclass7==4 | hisclass7==7

recode hisclass7 (1=1 "Upper Middle") (2=2 "Middle") (3=3 "Low Middle") (5=4 "Industrial workers") (4=5 "Farmers") (6=7 "Unskilled Urban") (7=6 "Low Skilled Farmers"), gen(hisclass7_reorder)
gen year_ctr=year-1921

recode hisclass7 (1/3=1 "Upper and middle") (5 6=2 "Industrial working class and lower") (4 7=3 "Agriculture"), gen(class_3)

gen id=_n

save "03 - Working data\marital_class.dta", replace

**------------------------------**
*---Preparation Data 1921-28----**
**------------------------------**

import delimited "01 - Original data\occupational_data_long_all_years_jan28.csv", clear
gen woman=1 if strpos(id, "Woman")
replace woman=0 if strpos(id, "Man")

gen marital=1 if strpos(id, "Wives")
replace marital=2  if strpos(id, "Professionals")
replace marital=3  if strpos(id, "Other_fmember")
replace marital=0 if strpos(id, "Man")


gen percent=1 if strpos(id, "Percent")

gen turnout=value if percent==1
destring turnout, replace force

encode hisclass7, gen(hisclass7_n)

gen married=marital==1
replace married=. if woman==0

gen group=1 if strpos(id, "Eligible")
replace group=2 if strpos(id, "Not_Elig")
replace group=3 if strpos(id, "Turnout") & strpos(id, "Number")
replace group=4 if strpos(id, "Turnout") & strpos(id, "Percent")
label define group 1"Eligible" 2"Not Eligible" 3"Turnout Number" 4"Turnout Percent"
label values group group

keep if marital!=.
egen id_lanyeargender=group( lan_code year cross_year_occup_code marital)

drop percent turnout
drop id

reshape wide value, i(id_lan) j(group)

rename value1 Eligible
rename value2 NotEligible
rename value3 Turnout_number
rename value4 Turnout_pct
destring Eligible- Turnout_pct, replace force
label define woman 0"Man" 1"Woman"
label define marital 0"Men" 1"Wives" 2"Professional women" 3"Unmarried, not f. employed"
label values woman woman
label values marital marital
order id_lanyeargender lan lan_code year year_occup cross_year_occup_code group_id hisclass7 hisclass descriptive hisclass7_n marital woman married

drop if lan_code=="Total"
destring lan_code, replace force
encode lan, gen(lan_num)

label define hisclass7 1"Higher" 2"Uppe middle" 3"Foremen and skilled" 4"Farmers and fishermen" 5"Lower skilled" 6"Unskilled urban" 7"Unskilled farmers"
label values hisclass7_n hisclass7 
drop married
recode marital 3=2, gen(married)
label define married 0"Man" 1"Married woman" 2"Unmarried woman"
label values married married

recode hisclass7_n 7=6 6=7
label define hisclass7_n2 1"Higher" 2"Uppe middle" 3"Foremen and skilled" 4"Farmers and fishermen" 5"Lower skilled" 6"Unskilled farmers" 7"Unskilled urban" 
label values hisclass7_n hisclass7_n2

gen stad=lan_num==23
replace stad=1 if lan_num==15
replace stad=1 if lan_num==6
label define stadb 1"Stad" 0"Län"
label values stad stadb

recode hisclass7_n (1/3=1 "Upper and middle") (5 7=2 "Industrial working class and lower") (4 6=3 "Agriculture"), gen(class_3)

save "03 - Working data\marital_class_lan.dta", replace

**------------------------------**
*---Preparation Data 1948-56----**
**------------------------------**

import excel "01 - Original data\sample48_56.xlsx", sheet("Sheet1") firstrow clear
encode habitat, gen( habitat_n)
encode Gender , gen( gender_n)
encode Marital , gen( marital_n)
recode marital_n (1=3 "Former Married") (2=3)(3=2 "Married") (4=1 "Unmarried"), gen(marital2)
drop marital_n
rename marital2 marital_n

drop habitat- Marital

gen yrbrn=year-Age
gen generation=1 if yrbrn<=1898
replace generation=2 if yrbrn>1898 & yrbrn<=1916
replace generation=3 if yrbrn>1916 & yrbrn<=1922
replace generation=4 if yrbrn>1922 & yrbrn<.
label define generation 1"Before the vote" 2"Interwar" 3"War" 4"Postwar"
label values generation generation

save "03 - Working data\sample48_56.dta", replace

**------------------------------**
*----Preparation 1960 Survey-----**
**------------------------------**

*use "01 - Original data\1960_election_survey.dta", clear
*renvars, upper

*keep V150 V14 V13 V195 V194 V212 V10 V11 V136 V97 V107 V118 V199 V200
*save "01 - Original data\1960_election_survey_short.dta", replace

use "01 - Original data\1960_election_survey_short.dta", clear
recode V150 5=0, gen(turnout1960)
gen rural=V14==8

gen yearbirth=V13
replace yearbirth=yearbirth+1800 if yearbirth>=80
replace yearbirth=yearbirth+1900 if yearbirth<80

recode yearbirth  (min/1899=1 "Before the vote") (1900/1917=2 "Interwar") (1918/1923=3 "War") (1924/max=4 "Postwar"), gen(cohort)

recode V195 (0 4=1 "Gainfully employed") (1/3=2 "Not gainfully employed"), gen(ocposition)
recode V194 (1/3=1 "Farming") (4/5=2 "Enterpreneurs and employees") (6=3 "Workers") (7=4 "Inactive"), gen(occupation)
recode V212 (0 1=1 "Upper and Middle") (3 10=3 "Agriculture") (4/6=1) (8/9= 2 "Working class"), gen(class3)
rename V10 gender
rename V11 marital
gen polint=V97*-1+5
gen polknow= V107 + V118
gen poldiscuss=V136 if V136!=8
recode V199 (1/5=1) (8=0) (.=.), gen(union)
recode V200 (1/6=1) (7=0) (.=.), gen(party)
drop V13- V212

save "03 - Working data\1960_election_survey.dta", replace

**------------------------------**
*----Preparation Union data-----**
**------------------------------**

use "01 - Original data\uniondata.dta", clear
destring Gender, replace
encode Gender, gen(gender_n)
des uniond
encode Descriptive2, gen(class)
recode class (1/3=1 "Upper and Middle") (4/5=2 "Working class and lower") (6/7=3 "Agriculture"), gen(class3)

save "03 - Working data\uniondata.dta", replace

